package com.yc.bdw.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yc.bdw.R;
import com.yc.bdw.model.Product;

import java.util.ArrayList;
import java.util.List;

public class PhoneShareListAdapter extends BaseAdapter {

	private List<Product> list = new ArrayList<Product>();
	private LayoutInflater inflater = null;
	private Context context = null;
	public PhoneShareListAdapter(){}

	public PhoneShareListAdapter(Context context) {
		inflater = LayoutInflater.from(context); 
		this.context = context;
	}
	
	@Override
	public int getCount() {
		if ( list != null ) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static class PhoneShareItem {
		ImageView img;
		TextView name;
		TextView price;
		TextView disparity;
		ImageView purchase;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		PhoneShareItem productItem;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_phone_share, null);
			productItem = new PhoneShareItem();
			productItem.img = (ImageView) convertView.findViewById(R.id.item_phoneshare_img);
			productItem.purchase = (ImageView) convertView.findViewById(R.id.item_phoneshare_purchase);
			productItem.name = (TextView) convertView.findViewById(R.id.item_phoneshare_name);
			productItem.price = (TextView) convertView.findViewById(R.id.item_phoneshare_price);
			productItem.disparity = (TextView) convertView.findViewById(R.id.item_phoneshare_disparity);

			convertView.setTag(productItem);
		} else {
			productItem = (PhoneShareItem) convertView.getTag();
		}

		productItem.name.setText(list.get(position).getName());
		productItem.price.setText("活动价：￥"+list.get(position).getPrice().toString());
		productItem.disparity.setText("比电脑购买省"+list.get(position).getDisparity()+"元");

//		String path = list.get(position).getImagePath();
//		productItem.img.getLayoutParams().width = ListUtils.dip2px(context, 120);
//		productItem.img.getLayoutParams().height = ListUtils.dip2px(context, 60);
//		KJBitmapUtils.getObj().display(productItem.img, path);

		productItem.purchase.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

			}
		});

		return convertView;
	}
	
	public List<Product> getList() {
		return list;
	}

	public void setList(List<Product> list) {
		this.list = list;
	}
}